require "public" 
local MJLogic = require "common/MJYFLogic"

local POKER = {
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --索子
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --索子
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --索子
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --索子
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --索子
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --索子
        --17 ,18,  19,  20,  37,  38,  39,  40,  41     
        0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       --索子                              
        -- 33 ,34,  35,  36,  37,  38,  39,  40,  41    
    -- 49  
    0x31,0x32,0x33,0x34,                                                --风牌(东南西北)
    0x31,0x32,0x33,0x34,                                                --风牌  
}

local MjDealer = class("MjDealer")

function MjDealer:ctor(host_id, xia_zhengjing)
	if host_id == 0 or host_id == nil then
		host_id = 1
	end
    self:reset_game(host_id, xia_zhengjing)
end

function MjDealer:reset_game(host_id, xia_zhengjing)
	if host_id == 0 or host_id == nil then
		host_id = 1
	end
    self.chairs_ = {}      
    self.rest_cards_ = {}  
    self.out_cards_ = {}   
    self.host_id_ = host_id
	if xia_zhengjing then 
		self.houtou_zhengjing = xia_zhengjing
	end
	self.tiandi_hu = true
	self.gangkai = false
	self.qianggang = false
	self.hu_id = 0
	self.dibei_ = 1
	self.log_data = { cards = {}, players = {}, room_info = {}, order = {} }
	self.jiesan_ = {}
	self.apply_jiesan_time = 0
	self.cur_host_id = host_id
	self.hu_users_  = {}
	self.gang_num = 0
	self.has_angang = false
	self.is_zhui_zhuang = 0
	self.is_zhui_zhuang_card = 0
end

function MjDealer:set_is_zhui_zhuang(tag)
	self.is_zhui_zhuang = tag
end
function MjDealer:get_is_zhui_zhuang()
	return self.is_zhui_zhuang
end

function MjDealer:set_is_zhui_zhuang_card(tag)
	self.is_zhui_zhuang_card = tag
end
function MjDealer:get_is_zhui_zhuang_card()
	return self.is_zhui_zhuang_card
end

function MjDealer:set_angang(tag)
	self.has_angang = tag
end

function MjDealer:get_angang()
	return self.has_angang
end


function MjDealer:set_cur_host(host_id, zhengjing)
	self.host_id_ = host_id
	self.houtou_zhengjing = zhengjing
end

function MjDealer:get_cur_host()
	return self.host_id_
end

function MjDealer:add_gang_num()
	self.gang_num = self.gang_num + 1
end

function MjDealer:get_gang_num()
	return self.gang_num
end

function MjDealer:add_jiesan(index)
	self.jiesan_[index] = 1	
end

function MjDealer:reset_jiesan(curr_time)
	self.jiesan_ = {}
	if curr_time then 
		self.apply_jiesan_time = os.time()
	else
		self.apply_jiesan_time = 0
	end
end

function MjDealer:get_jiesan()
	local length = 0
	for i,v in pairs(self.jiesan_) do
		length = length + 1
	end
	return length,self.jiesan_
end

function MjDealer:get_jiesan_time()
	return  self.apply_jiesan_time
end

function MjDealer:set_host(id)
    self.host_id_ = id
end

function MjDealer:get_host()
    return self.host_id_
end

function MjDealer:set_dibei(flag)
	self.dibei_ = flag
end

function MjDealer:get_dibei()
	return self.dibei_
end

function MjDealer:get_houtou_zhengjing()
    return self.houtou_zhengjing
end

function MjDealer:set_tiandi_hu(flag)
	self.tiandi_hu = flag
end

function MjDealer:is_tiandi_hu()
	return self.tiandi_hu
end

function MjDealer:set_gangkai(flag)
    self.gangkai = flag
end
function MjDealer:is_gangkai()
    return self.gangkai
end

function MjDealer:set_qianggang(flag)
    self.qianggang = flag
end
function MjDealer:is_qianggang()
    return self.qianggang
end

function MjDealer:shuffle(flag)
	math.randomseed(os.time())
    assert(type(POKER) == "table")
    local copy = {}
    for k, v in pairs(POKER) do  
        copy[k] = v
    end  
    local range = #POKER
    for k, v in pairs(POKER) do  
        local index = math.random(1, range)
        POKER[k] = table.remove(copy, index)
        range = range - 1
    end  

    self.rest_cards_ = clone(POKER)
    self.chairs_[1] = {}
    self.chairs_[2] = {}
    self.chairs_[3] = {}
    self.chairs_[4] = {}
	for i=1,13 do
        self.chairs_[1][i] = table.remove(self.rest_cards_, 1)
    end
	for i=1,13 do
        self.chairs_[2][i] = table.remove(self.rest_cards_, 1)
    end
	for i=1,13 do
        self.chairs_[3][i] = table.remove(self.rest_cards_, 1)
    end
	for i=1,13 do
        self.chairs_[4][i] = table.remove(self.rest_cards_, 1)
    end

    local cardd =  table.remove(self.rest_cards_, 1)  
    print('===tianhu=========cardd===='..cardd)
    if flag ~= nil and  flag == 1 then
		for i=1,99 do
		    print('===tianhu============='..i)
		    if self:tianhu( cardd ) == 1 then
		    	table.insert(self.rest_cards_,cardd)
		    	cardd =  table.remove(self.rest_cards_, 1) 
		    else
		    	break
			end
	    end 
    end 
    table.insert(self.chairs_[self.host_id_], cardd )
end
 
function MjDealer:tianhu(cardd_new)
	local hands_new = clone(self.chairs_[self.host_id_])
	table.insert(hands_new, cardd_new ) 
    if MJLogic.CanZimoHu(hands_new, {}, cardd_new, 1 ) ~= MJLogic.HU_NONE then
		hands_new = nil
    	return 1
	end
	hands_new = nil
	return 0
end

function MjDealer:get_initial_card(chair)
    return self.chairs_[chair]
end

function MjDealer:get_rest_card()
    return self.rest_cards_
end 

function MjDealer:get_rest_cards_count()
    return #self.rest_cards_
end

function MjDealer:draw_card(length, left_num,dihu_flag)  
	if length > left_num then  
		if dihu_flag ~= nil and dihu_flag == 0 then
		    local cardd =  table.remove(self.rest_cards_, 1)   
			for i=1,99 do
			    if self:tianhu( cardd ) ~= 1 then
			    	table.insert(self.rest_cards_,cardd)
			    	cardd =  table.remove(self.rest_cards_, 1) 
			    else
			    	break
				end
		    end  
		    return cardd
		else
			return table.remove(self.rest_cards_, 1)  
		end
	else
		return nil
	end
end

function MjDealer:show_rand_card(length)
	local index = math.random(1, length)
	return self.rest_cards_[index]
end

function MjDealer:out_card(data)
    table.insert(self.out_cards_, data)
end

function MjDealer:reset_out_card()
    table.remove(self.out_cards_)
end

function MjDealer:add_hu_user(id)
	table.insert(self.hu_users_,id)
end

function MjDealer:get_hu_users()
	return self.hu_users_
end

function MjDealer:set_hu_id(id)
	self.hu_id = id
end

function MjDealer:get_hu_id()
	return self.hu_id
end

function MjDealer:get_hu_cards()
	return self.hu_cards_
end

function MjDealer:set_log_cards(cards)
	self.log_data.cards = cards
end

function MjDealer:set_log_players(players)
	self.log_data.players = players
end

function MjDealer:set_log_room_info(room_info)
	self.log_data.room_info = room_info
end

function MjDealer:add_log_order(cmd)
	return table.insert(self.log_data.order,cmd)
end

function MjDealer:get_log_data()
	return self.log_data
end

function MjDealer:clean_log_data()
	self.log_data = { cards = {}, players = {}, room_info = {}, order = {} }
end

return MjDealer
